package com.atguigu.listener;

import com.rabbitmq.client.AMQP;
import com.rabbitmq.client.Channel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.io.IOException;

@Component
public class MyMessageListener {
    private static final Logger log = LoggerFactory.getLogger(MyMessageListener.class);

    @RabbitListener(queues = "atguigu.queue.simple")
    public void myMessageSimple(String msg, Channel channel ){
        System.out.println(msg);
    }

    @RabbitListener(queues = "atguigu.queue.woek")
    public void myMessageWork(String msg, Channel channel ){
        System.out.println(msg);
    }

    @RabbitListener(queues = "atguigu.queue.fanout01")
    public void myMessageFanout01(String msg, Message message,Channel channel){
        System.out.println("01 "+ message);
    }

    @RabbitListener(queues = "atguigu.queue.fanout02")
    public void myMessageFanout02(String msg, Message message,Channel channel){
        System.out.println("02 "+ message);
    }

    @RabbitListener(queues = "atguigu.queue.direct")
    public void myMessageDirect(String msg, Message message,Channel channel){
        System.out.println("direct01"+ msg);
    }

    @RabbitListener(queues = "atguigu.queue.direct2")
    public void myMessageDirect02(String msg, Message message,Channel channel){
        System.out.println("direct02"+ msg);
    }

    @RabbitListener(queues = "atguigu.queue.message")
    public void myMessageTopic(String msg, Message message,Channel channel){
        System.out.println("direct01"+ msg);
    }

    @RabbitListener(queues = "atguigu.queue.order")
    public void myMessageTopic02(String msg, Message message,Channel channel){
        System.out.println("direct02"+ msg);
    }


    @RabbitListener(
            // 设置绑定关系
            bindings = @QueueBinding(
            // 配置队列信息：durable 为 true 表示队列持久化；autoDelete 设置为 false 表示关闭自动删除
            value = @Queue(value = "hang",durable = "true",autoDelete = "false"),
            // 配置交换机信息：durable 为 true 表示队列持久化；autoDelete 设置为 false 表示关闭自动删除
            exchange = @Exchange(value = "deng",durable = "true",autoDelete = "false"),
            // 配置路由键信息
            key = {"bin"}
    ))
    public void processMessage(String msg,Message message,Channel channel) throws IOException {
        // 1、获取当前消息的 deliveryTag 值备用,类似于id标识
        long deliveryTag = message.getMessageProperties().getDeliveryTag();

        try {
            // 2、正常业务操作
            log.info(msg);
           // int i= 1/0;
           // Thread.sleep(1000);
            // 3、给 RabbitMQ 服务器返回 ACK 确认信息
            channel.basicAck(deliveryTag,false);
        } catch (Exception e) {
            // 4、获取信息，看当前消息是否曾经被投递过
            Boolean redelivered = message.getMessageProperties().getRedelivered();
            if (!redelivered){
                // 5、如果没有被投递过，那就重新放回队列，重新投递，再试一次
                channel.basicNack(deliveryTag,false,true);
            }
            // 6、如果已经被投递过，且这一次仍然进入了 catch 块，那么返回拒绝且不再放回队列
            channel.basicReject(deliveryTag,false);
        }
    }


}
